<script setup>
import {
  indexaction as indexactionApi,
  currentaction as currentactionApi,
} from "@/api/category";
import { onBeforeMount, reactive } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();
const state = reactive({
  categoryList: [],
  activeId: "",
  currentSubListData: {},
});
onBeforeMount(async () => {
  const res = await indexactionApi();
  state.categoryList = res.categoryList;
  state.activeId = res.categoryList[0].id;
  getSubListData(state.activeId);
});
const getSubListData = async (id) => {
  const res = await currentactionApi({ id });
  state.currentSubListData = res.data.currentOne;
};

// 处理一级分类的点击事件
const handleCategoryClick = async (id) => {
  state.activeId = id;
  getSubListData(id);
};
// 点击跳转到二级分类页面
const goSubCategory = (id) => {
  router.push({ path: "/subcategory", query: { id } });
};
</script>

<template>
  <div class="category-page">
    <!-- 搜索框 -->
    <!-- 分类 -->
    <div class="category-container">
      <div class="category-left">
        <div
          class="item"
          :class="{ active: state.activeId === item.id }"
          v-for="item in state.categoryList"
          :key="item.id"
          @click="handleCategoryClick(item.id)"
        >
          {{ item.name }}
        </div>
      </div>
      <div class="category-right">
        <div class="banner-containere">
          <van-image
            class="banner-img"
            :src="state.currentSubListData.banner_url"
            fit="contain"
          />
        </div>
        <div class="title">{{ state.currentSubListData.name }}分类</div>
        <div class="list-container">
          <div
            class="sub-item"
            v-for="item in state.currentSubListData.subList"
            :key="item.id"
            @click="goSubCategory(item.id)"
          >
            <van-image class="img" :src="item.wap_banner_url" fit="contain" />
            <div class="category-name">{{ item.name }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.category-page {
  .category-container {
    display: flex;
    .category-left {
      height: calc(100vh - 99px);
      width: 164px;
      background-color: #fff;
      .item {
        margin-bottom: 64px;
        font-family: PingFangSC-Regular;
        font-size: 30px;
        text-align: center;
        &:first-child {
          margin-top: 60px;
        }
      }
      .active {
        font-family: PingFangSC-Semibold;
        color: #cea62a;
        font-weight: 600;
      }
    }
    .category-right {
      flex: 1;
      .banner-containere {
        padding: 20px 20px 0 20px;
        .banner-img {
          width: 546px;
          height: 217.85px;
          display: block;
        }
      }
      .title {
        text-align: center;
        font-family: PingFangSC-Medium;
        font-size: 30px;
        font-weight: 500;
        margin-top: 40px;
        margin-bottom: 28.5px;
        position: relative;
        &::after {
          position: absolute;
          bottom: 4px;
          left: 50%;
          z-index: -1;
          transform: translateX(-50%);
          display: block;
          content: "";
          width: 120px;
          height: 14px;
          background-color: #b7a673;
        }
      }
      .list-container {
        padding: 0 48.295px;
        display: flex;
        justify-content: flex-start;
        align-items: flex-start;
        align-content: flex-start;
        flex-wrap: wrap;
        .sub-item {
          margin-right: 35px;
          margin-bottom: 38.5px;
          text-align: center;
          &:nth-child(3n + 3) {
            margin-right: 0;
          }
          .img {
            width: 137px;
            height: 137px;
          }
          .category-name {
            font-family: PingFangSC-Medium;
            font-size: 24px;
            font-weight: 500;
          }
        }
      }
    }
  }
}
</style>
